<%@ page import="java.util.List" %>
<%@ page import="com.bookstore.entity.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/favicon.ico">
    <link rel="canonical" href="https://getbootstrap.com/docs/3.4/examples/dashboard/">
    <%@ include file="../plugs.jsp"%>
    <title>在线书城后台管理系统</title>
    <!-- Bootstrap core CSS -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/dist/css/bootstrap.min.css"
          rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/css/ie10-viewport-bug-workaround.css"
          rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/examples/dashboard/dashboard.css"
          rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/ie8-responsive-file-warning.js"></script>
    <![endif]-->
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false"
                    aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">在线书城后台管理系统</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">设置</a></li>
                <li><a href="#">退出</a></li>
            </ul>
            <form class="navbar-form navbar-right">
                <input type="text" class="form-control" placeholder="搜索">
            </form>
        </div>
    </div>
</nav>

<div class="container-fluid">
    <div class="row">
        <%@ include file="sideBar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">


            <h2 class="sub-header">用户列表</h2>
            <!-- 绑定模态框按钮 -->
            <button class="btn btn-primary" data-toggle="modal" data-target="#productModal" id="insertbtn">添加商品</button>
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>用户名</th>
                        <th>密码</th>
                        <th>性别</th>
                        <th>邮箱</th>
                        <th>电话</th>
                        <th>个人介绍</th>
                        <th>验证码</th>
                        <th>状态</th>
                        <th>角色</th>
                        <th>注册时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <%
                        Object users = request.getAttribute("users");
                        if ("users为空".equals(users.toString())) {
                    %>
                    }
                    <%="users为空"%>
                    <%
                    } else {
                        int i = 1;
                        List<User> list = (List<User>) users;
                        for (User user : list) {
                    %>
                    <tr>
                        <th><%=i%>
                        </th>
                        <th><%=user.getUsername()%>
                        </th>
                        <th><%=user.getPassword()%>
                        </th>
                        <th><%=user.getGender()%>
                        </th>
                        <th><%=user.getEmail()%>
                        </th>
                        <th><%=user.getTelephone()%>
                        </th>
                        <th><%=user.getIntroduce()%>
                        </th>
                        <th><%=user.getActiveCode()%>
                        </th>
                        <th><%=user.getState()%>
                        </th>
                        <th><%=user.getRole()%>
                        </th>
                        <th><%=user.getRegistTime()%>
                        </th>
                        <th>
                            <a type="button" class="btn btn-link"
                               href="./manager.do?action=updateuser&id=<%=user.getId()%>">重置密码</a>
                            <a type="button" class="btn btn-link"
                               href="./manager.do?action=deleteuser&id=<%=user.getId()%>">删除</a>
                        </th>
                    </tr>
                    <%
                                i++;
                            }
                        }
                    %>

                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- 商品模态框部分 -->
<!-- Modal -->
<div class="modal fade" id="productModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close" id="updateclosemodel"><span
                        aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">用户添加</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-1"></div>
                    <div class="col-md-10">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label for="username" class="col-sm-3 control-label">用户名</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" id="username" name="username"
                                           placeholder="用户名">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">性别</label>
                                <div class="col-sm-9">
                                    <input type="radio" class="input-lg" id="man" name="gender" checked style="width: 20px;height: 20px;">
                                    <label for="man" style="display: inline-block;height: 20px;line-height: 5px;font-size: 20px;">男</label>
                                    <input type="radio" class="input-lg" id="woman" name="gender" style="width: 20px;height: 20px;">
                                    <label for="woman" style="display: inline-block;height: 20px;line-height: 5px;font-size: 20px;">女</label>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="telephone" class="col-sm-3 control-label">电话号码</label>
                                <div class="col-sm-9">
                                    <input type="number" class="form-control" id="telephone" name="telephone"
                                           placeholder="电话号码">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="email" class="col-sm-3 control-label">邮箱</label>
                                <div class="col-sm-9">
                                    <input type="email" class="form-control" id="email" name="email" placeholder="邮箱">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="introduce" class="col-sm-3 control-label">个人介绍</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" id="introduce" name="introduce"
                                              placeholder="个人介绍"></textarea>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="col-md-1"></div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="btnsave">保存</button>
            </div>
        </div>
    </div>
</div>

<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"
        integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
        crossorigin="anonymous"></script>
<script>
    window.jQuery || document.write(
        '<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/vendor/jquery.min.js"><\/script>'
    )
</script>
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/dist/js/bootstrap.min.js"></script>
<!-- Just to make our placeholder images work. Don't actually copy the next line! -->
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/vendor/holder.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.14/assets/js/ie10-viewport-bug-workaround.js"></script>
<script type="text/javascript" src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<script>
    $(function () {
        $('div').on('click', '#insertbtn', function () {
            $('#myModalLabel').text("用户添加");
            $('#username').val("");
            $('#gender').val("");
            $('#telephone').val("");
            $('#email').val("");
            $('#introduce').val("");
        });
        $('#btnsave').click(function () {
            var username = $('#username').val();
            var gender = $('#gender').val();
            var telephone = $('#telephone').val();
            var email = $('#email').val();
            var introduce = $('#introduce').val();
            var action = 'addUser';
            $.ajax({
                async: true,
                url: "./manager.do",
                type: "POST",
                data: {
                    action: action,
                    username: username,
                    gender: gender,
                    email: email,
                    telephone: telephone,
                    introduce: introduce
                },
                success: function (data) {
                    if (data == null || data == "" || data == undefined) {
                        $('#updateclosemodel')[0].class = close;
                        return;
                    }
                    var data1 = JSON.parse(data);
                    if (data1 == 1) {
                        $('#updateclosemodel')[0].class = close;
                    }
                    alertFun(data1, '添加');
                },
                dataType: "text"
            });
        });

    })

    function alertFun(num, txt) {
        if (num == 0) {
            Swal.fire({
                type: 'error',
                title: '失败提示.',
                text: txt + '失败!',
                footer: '<a href>请检输入数据是否正确</a>'
            })
        } else {
            Swal.fire({
                position: 'top-middle',
                type: 'success',
                title: txt + '成功！',
                showConfirmButton: true,
                timer: 2500
            }).then(function (isOk){
                if (isOk){
                    location.href = "./manager.do?action=queryAllUser";
                }
            })

        }
    }
</script>
</body>
</html>
